var db;
var k_id = getUrlVars()["id"];


// connect to database
function connectDB() {
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",
			500000);
	db.transaction(getKanjiDetail, errorCB);
}

// function will be called when an error occurred
function errorCB(err) {
	alert("Error processing SQL: " + err.code);
}

function successCB() {
	db.transaction(getKanjiDetail, errorCB);
}



// get all lesson from db
function getKanjiDetail(tx) {
	var sql = "SELECT k_id, k_kanji, k_mean_kanji, k_mean_en, k_mean_vi, k_onyomi, k_kunyomi, k_remember, k_on_ex, k_kun_ex,k_favourite, k_dataversion_id"
			+ " FROM kanji "
			+ " WHERE k_id =? ";
	tx.executeSql(sql, [k_id], showKanjiDetail);
}

function showKanjiDetail(tx,results) {
	$('#kanji').append('<strong>'+results.rows.item(0).k_kanji+'</strong>');
	$('#mean_vi').append(results.rows.item(0).k_mean_vi);
	$('#mean_en').append(results.rows.item(0).k_mean_en);
	$('#onyomi').append(results.rows.item(0).k_onyomi);
	$('#kunyomi').append(results.rows.item(0).k_kunyomi);
	$('#remember').append(results.rows.item(0).k_remember);
	
	
	var kun_exampleArr = results.rows.item(0).k_kun_ex.split('|');
	for(var i=0;i<kun_exampleArr.length;i++){
		var itemChild = kun_exampleArr[i].split(':');
		$('#kun_example').append('<tr>');
		for(var j=0;j<itemChild.length;j++){
			//console.log('child thu ' + j + ' la : ' +itemChild[j]); 
			$('#kun_example').append('<td>'+itemChild[j]+'</td>');
		}
		$('#kun_example').append('</tr>');
    }
	
	var on_exampleArr = results.rows.item(0).k_on_ex.split('|');
	for(var i=0;i<on_exampleArr.length;i++){
		var itemChild = on_exampleArr[i].split(':');
		$('#on_example').append('<tr>');
		for(var j=0;j<itemChild.length;j++){
			//console.log('child thu ' + j + ' la : ' +itemChild[j]); 
			$('#on_example').append('<td>'+itemChild[j]+'</td>');
		}
		$('#on_example').append('</tr>');
    }
	
	//set icon cho favourite button 
	if (results.rows.item(0).k_favourite == 1) {
		$('a[task="favouriteTask"]').attr("id","addFavourite");
	}else{
		$('a[task="favouriteTask"]').attr("id"," ");
	}
}

